#include "BPageSwitchContainer.h"
#include <QPushButton>


BPageSwitchContainer::BPageSwitchContainer(QWidget *parent) :
    QWidget(parent)
{
    this->resize(parent->size());

    //设置按钮透明度
    m_opacity = new QGraphicsOpacityEffect;
    m_opacity->setOpacity(1);

//    BPageSwitch* page1 = new BPageSwitch(this);
//    BPageSwitch* page2 = new BPageSwitch(this);
//    BPageSwitch* page3 = new BPageSwitch(this);

//    addPage(page1);
//    addPage(page2);
//    addPage(page3);

//    for (int i = 0; i < m_page.size(); i++) {
//        QLabel* label = new QLabel(m_page[i]);
//        label->setText("Page" + QString::asprintf("%d", m_page[i]->getID()));
//        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
//        m_page[i]->layout()->addWidget(label);
//    }

//    QPushButton* button = new QPushButton(this);
//    button->setText("Test");
//    button->resize(100, 50);
//    button->move(this->width()/2, this->height() - button->height());
//    button->show();

//    setCenterPage(1);
}

BPageSwitchContainer::~BPageSwitchContainer()
{
    for (auto& p : m_page) {
        delete p;
    }
}

void BPageSwitchContainer::addPage(BPageSwitch* page)
{
    m_page.push_back(page);
    m_pageNum++;    //数量加一，设置page属性
    if (m_pageNum == 1) {
        m_size.push_back(m_centerSize); //第一个为中心大小
        m_point.push_back(m_centerPoint);   //中心位置
    }
    else {
        m_size.push_back(m_elseSize);
        m_point.push_back(m_elsePoint + (m_pageNum - 2)*QPoint(0, m_elseSize.height()));
    }

    page->setID(m_pageNum-1);
    page->setSite(m_pageNum-1);
    page->setGraphicsEffect(m_opacity);
    //绑定事件
    connect(page, &BPageSwitch::requestCenter,
            this, &BPageSwitchContainer::setCenterPage);
}


void BPageSwitchContainer::setCenterPage(int pageID)
{
    //确认中心
    if (m_centerPageID != -1)
        m_page[m_centerPageID]->resetCenter();
    m_page[pageID]->setCenter();
    m_centerPageID = pageID;
    /*
    * @Brief: 当前页面id应去的下一个id名
    */
    auto nextSite = [=](int site){
        return (site == 0) ? m_page.size()-1 : site - 1;
    };
    int moveTimes = m_page[pageID]->getSite(); //每个页面的移动次数（为0时表示初始化，需要移动一次）
    /*******遍历所有id，从当前site变化为nextSite的位置，共移动moveTimes次*******/
    QSequentialAnimationGroup* sqGroup = new QSequentialAnimationGroup;//顺序动画组
    //透明度动画
    QPropertyAnimation* fade = new QPropertyAnimation(m_opacity, "opacity");
    fade->setDuration(750);
    fade->setStartValue(0);
    fade->setEndValue(0.99);
    for (int times = 0; times < moveTimes; times++) {
        QParallelAnimationGroup* dpGroup = new QParallelAnimationGroup(sqGroup);//平行动画组
        for (int id = 0; id < m_page.size(); id++) {
            //移动与放缩动画
            QPropertyAnimation* animPos = new QPropertyAnimation(m_page[id], "pos", dpGroup);
            QPropertyAnimation* animSize = new QPropertyAnimation(m_page[id], "size", dpGroup);
//            for (int times = 0; times < moveTimes; times++)
                m_page[id]->setSite( nextSite(m_page[id]->getSite()) );
            //pos
            animPos->setStartValue(m_page[id]->pos());
            animPos->setEndValue( m_point[ m_page[id]->getSite() ] );
            animPos->setDuration(600);
            animPos->setEasingCurve(QEasingCurve::InOutExpo);
            //size
            animSize->setStartValue(m_page[id]->size());
            animSize->setEndValue(m_size[ m_page[id]->getSite() ]);
            animSize->setDuration(600);
            animSize->setEasingCurve(QEasingCurve::InOutExpo);
            //动画组
            dpGroup->addAnimation(animPos);
            dpGroup->addAnimation(animSize);
        }
        dpGroup->addAnimation(fade);
        sqGroup->addAnimation(dpGroup);
    }
    sqGroup->start(QAbstractAnimation::DeletionPolicy::DeleteWhenStopped);
}

